# Eagle Project - Production Deployment Script
# 使用方法: .\scripts\deploy-prod.ps1

param(
    [string]$Environment = "prod",
    [string]$ConfigFile = "config/config.prod.yaml",
    [switch]$BuildOnly,
    [switch]$DockerOnly
)

Write-Host "========================================" -ForegroundColor Green
Write-Host "Eagle Project - Production Deployment" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# 检查 PowerShell 版本
if ($PSVersionTable.PSVersion.Major -lt 5) {
    Write-Host "错误: 需要 PowerShell 5.0 或更高版本" -ForegroundColor Red
    exit 1
}

# 检查 Go 环境
Write-Host "[1/6] 检查 Go 环境..." -ForegroundColor Yellow
try {
    $goVersion = go version
    Write-Host "✓ $goVersion" -ForegroundColor Green
} catch {
    Write-Host "错误: 未找到 Go 环境" -ForegroundColor Red
    exit 1
}

# 检查配置文件
Write-Host "[2/6] 检查配置文件..." -ForegroundColor Yellow
if (-not (Test-Path $ConfigFile)) {
    Write-Host "错误: 配置文件 $ConfigFile 不存在" -ForegroundColor Red
    Write-Host "请先创建生产环境配置文件" -ForegroundColor Yellow
    exit 1
}
Write-Host "✓ 配置文件存在" -ForegroundColor Green

# 清理旧的构建文件
Write-Host "[3/6] 清理构建文件..." -ForegroundColor Yellow
if (Test-Path "bin") {
    Remove-Item -Path "bin" -Recurse -Force
}
New-Item -ItemType Directory -Path "bin" -Force | Out-Null
Write-Host "✓ 构建目录已清理" -ForegroundColor Green

# 生成 Wire 代码
Write-Host "[4/6] 生成 Wire 代码..." -ForegroundColor Yellow
try {
    Push-Location "cmd/server"
    wire
    if ($LASTEXITCODE -ne 0) {
        throw "Wire 生成失败"
    }
    Pop-Location
    
    Push-Location "cmd/consumer"
    wire
    if ($LASTEXITCODE -ne 0) {
        throw "Wire 生成失败"
    }
    Pop-Location
    
    Write-Host "✓ Wire 代码生成完成" -ForegroundColor Green
} catch {
    Write-Host "错误: $_" -ForegroundColor Red
    exit 1
}

# 构建应用
Write-Host "[5/6] 构建应用..." -ForegroundColor Yellow
try {
    # 构建 Linux 版本 (用于 Docker)
    $env:GOOS = "linux"
    $env:GOARCH = "amd64"
    $env:CGO_ENABLED = "0"
    
    go build -ldflags "-w -s" -o bin/eagle-service cmd/server/main.go cmd/server/wire_gen.go
    if ($LASTEXITCODE -ne 0) {
        throw "应用构建失败"
    }
    
    # 构建 Windows 版本 (用于本地测试)
    $env:GOOS = "windows"
    $env:GOARCH = "amd64"
    go build -ldflags "-w -s" -o bin/eagle-service.exe cmd/server/main.go cmd/server/wire_gen.go
    if ($LASTEXITCODE -ne 0) {
        throw "Windows 版本构建失败"
    }
    
    # 构建消费者
    $env:GOOS = "linux"
    go build -ldflags "-w -s" -o bin/eagle-consumer cmd/consumer/main.go cmd/consumer/wire_gen.go
    if ($LASTEXITCODE -ne 0) {
        throw "消费者构建失败"
    }
    
    Write-Host "✓ 应用构建完成" -ForegroundColor Green
} catch {
    Write-Host "错误: $_" -ForegroundColor Red
    exit 1
}

if ($BuildOnly) {
    Write-Host ""
    Write-Host "构建完成，文件位置:" -ForegroundColor Green
    Write-Host "  - bin/eagle-service (Linux)" -ForegroundColor Cyan
    Write-Host "  - bin/eagle-service.exe (Windows)" -ForegroundColor Cyan
    Write-Host "  - bin/eagle-consumer (Linux)" -ForegroundColor Cyan
    exit 0
}

# Docker 部署
Write-Host "[6/6] Docker 部署..." -ForegroundColor Yellow
try {
    # 检查 Docker
    docker version | Out-Null
    if ($LASTEXITCODE -ne 0) {
        throw "Docker 未安装或未启动"
    }
    
    # 构建 Docker 镜像
    Write-Host "构建 Docker 镜像..." -ForegroundColor Cyan
    docker build -t eagle-service:latest -f deploy/docker/Dockerfile .
    if ($LASTEXITCODE -ne 0) {
        throw "Docker 镜像构建失败"
    }
    
    # 创建 .env 文件
    $envContent = @"
# 生产环境配置
MYSQL_ROOT_PASSWORD=your_secure_mysql_root_password
MYSQL_PASSWORD=your_secure_mysql_password
REDIS_PASSWORD=your_secure_redis_password
"@
    
    $envFile = "deploy/docker-compose/.env"
    $envContent | Out-File -FilePath $envFile -Encoding UTF8
    Write-Host "✓ 环境变量文件已创建: $envFile" -ForegroundColor Green
    
    # 启动服务
    if (-not $DockerOnly) {
        Write-Host "启动生产环境服务..." -ForegroundColor Cyan
        Push-Location "deploy/docker-compose"
        docker-compose -f docker-compose.prod.yaml up -d
        if ($LASTEXITCODE -ne 0) {
            throw "服务启动失败"
        }
        Pop-Location
        
        Write-Host "✓ 生产环境部署完成" -ForegroundColor Green
        Write-Host ""
        Write-Host "服务信息:" -ForegroundColor Yellow
        Write-Host "  - 应用服务: http://localhost:8080" -ForegroundColor Cyan
        Write-Host "  - 健康检查: http://localhost:8080/health" -ForegroundColor Cyan
        Write-Host "  - Nginx: http://localhost:80" -ForegroundColor Cyan
        Write-Host ""
        Write-Host "查看服务状态:" -ForegroundColor Yellow
        Write-Host "  docker-compose -f deploy/docker-compose/docker-compose.prod.yaml ps" -ForegroundColor Cyan
        Write-Host ""
        Write-Host "查看日志:" -ForegroundColor Yellow
        Write-Host "  docker-compose -f deploy/docker-compose/docker-compose.prod.yaml logs -f" -ForegroundColor Cyan
    } else {
        Write-Host "✓ Docker 镜像构建完成" -ForegroundColor Green
        Write-Host "使用以下命令启动服务:" -ForegroundColor Yellow
        Write-Host "  cd deploy/docker-compose" -ForegroundColor Cyan
        Write-Host "  docker-compose -f docker-compose.prod.yaml up -d" -ForegroundColor Cyan
    }
    
} catch {
    Write-Host "错误: $_" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "部署完成!" -ForegroundColor Green 